package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.cost.*;
import com.hllxd.hism.star.basic.service.service.CostReportService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/cost_report")
public class CostReportController2 {
    @Autowired
    private CostReportService costReportService;

    @Data
    public static class Params {
        public Long id;
        /**
         * store,task
         */
        public String type = "task";
        /**
         * dt 报告时间
         */
        public String dt;
        public String queueName;
        public String basicPath;
        /**
         * 邮箱前缀
         */
        public String ownerId;
        public String userOa;

        /**
         * buCode 业务团队
         */
        public String buCode;
        /**
         * 周期 day, week, month
         */
        public String period;

        public Boolean isAll;
    }

//    @RestGrpcMapping("/listCostReportTeam.do")
//    public NTSTATUS listCostReportTeam(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Params aa = params.driverObject(Params.class);
//            if ("store".equals(aa.type)) {
//                result.setData(costReportService.listCostTableReport4Team(aa.dt, aa.basicPath));
//            }else {
//                result.setData(costReportService.listCostTaskReport4Team(aa.dt, aa.queueName));
//            }
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

    @RestGrpcMapping("/listCostReport4Team.do")
    public NTSTATUS listCostReport4Team(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            Params aa = params.driverObject(Params.class);
            if ("store".equals(aa.type)) {
                result.setData(costReportService.listCostTableReport4Team(aa.dt, aa.basicPath));
            }else {
                result.setData(costReportService.listCostTaskReport4Team(aa.dt, aa.queueName));
            }

            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listCostReport4Owner.do")
    public NTSTATUS listCostReport4Owner(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            Params aa = params.driverObject(Params.class);
            if ("store".equals(aa.type)) {
                result.setData(costReportService.listCostTableReport4Owner(aa.dt, aa.ownerId, aa.basicPath));
            }else {
                result.setData(costReportService.listCostTaskReport4Owner(aa.dt, aa.queueName, aa.ownerId));
            }
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listCostTaskReportDetail.do")
    public NTSTATUS listCostTaskReportDetail(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(costReportService.listCostTaskReportDetail(aa.dt,aa.queueName,aa.ownerId));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    @RestGrpcMapping("/listCostTableReportDetail.do")
    public NTSTATUS listCostTableReportDetail(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            List<CostReportDetailEntity> res = costReportService.listCostTableReportDetail(aa.dt, aa.ownerId, aa.basicPath);
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/loadCostTaskReportTrend.do")
    public NTSTATUS loadCostTaskReportTrend(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            CostTaskEntity aa = params.driverObject(CostTaskEntity.class);
            CostTaskDetailChartEntity res;
            res = costReportService.loadCostTaskReportTrend(aa);
            log.info("success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/loadCostTableReportTrend.do")
    public NTSTATUS loadCostTableReportTrend(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
//            result.setData(yarnApplicationService.listYarnApplicationNow(paramMap)
            CostUserChartEntity chart = costReportService.loadCostTableReportTrend(aa.ownerId,aa.basicPath);
            result.setData(chart);
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listCostQuota.do")
    public NTSTATUS listCostQuota(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            result.setData(costReportService.listCostQuota());
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/loadCostQuota.do")
    public NTSTATUS loadCostQuota(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(costReportService.loadCostQuota(aa.id));
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/setCostQuota.do")
    public NTSTATUS setCostQuota(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            CostQuotaEntity aa = params.driverObject(CostQuotaEntity.class);
            result.setData(costReportService.setCostQuota(aa));
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }





}
